package com.cyl.manager.oms.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.manager.oms.domain.entity.Order;
import com.cyl.manager.oms.domain.entity.OrderItemPrepare;
import com.cyl.manager.oms.domain.form.OrderItemPrepareFrom;
import com.cyl.manager.oms.domain.query.OrderItemPrepareQuery;
import com.cyl.manager.oms.domain.vo.OrderGysVo;
import com.cyl.manager.oms.domain.vo.OrderItemPrepareExcelVO;
import com.cyl.manager.oms.mapper.OrderItemMapper;
import com.cyl.manager.oms.mapper.OrderItemPrepareMapper;
import com.cyl.manager.oms.mapper.OrderMapper;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.service.MyBaseService;
import com.ruoyi.system.service.ISysDeptService;

import io.swagger.annotations.ApiModelProperty;

/**
 * 备货管理Service业务层处理
 *
 *
 * @author zcc
 */
@Service

public class OrderItemPrepareService extends MyBaseService<OrderItemPrepareMapper, OrderItemPrepare>{
    @Autowired
    private OrderItemPrepareMapper orderItemPrepareMapper;
    
    @Autowired
    private OrderMapper orderMapper;
    
    @Autowired
    private OrderItemMapper orderItemMapper;
    
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询备货管理
     *
     * @param id 备货管理主键
     * @return 备货管理
     */
    public OrderItemPrepare selectById(Long id) {
        return orderItemPrepareMapper.selectById(id);
    }
    
    
    
    /**
     * 查询备货管理列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 备货管理
     */
    public Map<String, Object> selectListByExcel(OrderItemPrepareQuery query, Pageable page) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	List<OrderItemPrepare> list = selectList(query, page);
    	List<OrderItemPrepareExcelVO> listRet = new ArrayList<OrderItemPrepareExcelVO>();
		boolean booltj = false;
    	if("true".equals(query.getH5tj())) {
    		booltj = true;
		}
    	
    	if(list!=null) {
    		for (OrderItemPrepare oip : list) {
    			OrderItemPrepareExcelVO evo = new OrderItemPrepareExcelVO();
    			evo.setId(oip.getId());
    			evo.setProductName(oip.getProductName());
    			evo.setSpData(oip.getSpData());
    			evo.setSkuSum(oip.getSkuSum());
    			evo.setSalePrice(oip.getSalePrice());
    			evo.setStartTime(oip.getStartTime());
    			evo.setBhZt(oip.getBhZt());
    			evo.setLockId(oip.getLockId());
    			evo.setPic(oip.getPic());
    			evo.setEndTime(oip.getEndTime());
    			evo.setExpressmc(oip.getExpressmc());
    			evo.setExpressbh(oip.getExpressbh());
    			evo.setCreateTime(oip.getCreateTime());
    			evo.setOrdernum(oip.getOrdernum());
    			evo.setDeptZbjMc(oip.getDeptZbjMc());
    			evo.setDeptDpMc(oip.getDeptDpMc());
    			evo.setDpdh(oip.getDpdh());
    			evo.setDplxr(oip.getDplxr());
    			evo.setAddress(oip.getAddress());
    			if(booltj) {
    		        QueryWrapper<Order> queryWrapper = new QueryWrapper<Order>();
    		        queryWrapper.eq("lock_id", oip.getLockId());
    		        queryWrapper.eq("prepare_id", oip.getId());
    		        queryWrapper.eq("send_gys_status", 4);
    		        evo.setDhxordernum(orderMapper.selectCount(queryWrapper));
    		        evo.setDhxspnum(orderItemMapper.getOrderProductDpSum(oip.getLockId(), oip.getId()));
    			}
    			listRet.add(evo);
    		     
			}
    	}
    	map.put("list", listRet);
    	if(page==null) {
        	map.put("count", listRet.size());
    	}else {
        	map.put("count", ((com.github.pagehelper.Page)list).getTotal());
    	}
		return map;
    }

    /**
     * 查询备货管理列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 备货管理
     */
    public List<OrderItemPrepare> selectList(OrderItemPrepareQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<OrderItemPrepare> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        Long id = query.getId();
        if (id != null) {
            qw.eq("id", id);
        }
        Long deptZbjId = query.getDeptZbjId();
        if (deptZbjId != null) {
            qw.eq("dept_zbj_id", deptZbjId);
        }
        Long deptTzId = query.getDeptTzId();
        if (deptTzId != null) {
            qw.eq("dept_tz_id", deptTzId);
        }
        Long deptDpId = query.getDeptDpId();
        if (deptDpId != null) {
            qw.eq("dept_dp_id", deptDpId);
        }
        LocalDateTime startTime = query.getStartTime();
        if (startTime != null) {
            qw.eq("start_time", startTime);
        }
        LocalDateTime endTime = query.getEndTime();
        if (endTime != null) {
            qw.eq("end_time", endTime);
        }
        Long productId = query.getProductId();
        if (productId != null) {
            qw.eq("product_id", productId);
        }
        String productNameLike = query.getProductNameLike();
        if (!StringUtils.isEmpty(productNameLike)) {
            qw.like("product_name", productNameLike);
        }
        Long skuId = query.getSkuId();
        if (skuId != null) {
            qw.eq("sku_id", skuId);
        }
        String outSkuId = query.getOutSkuId();
        if (!StringUtils.isEmpty(outSkuId)) {
            qw.eq("out_sku_id", outSkuId);
        }
        String spData = query.getSpData();
        if (!StringUtils.isEmpty(spData)) {
            qw.eq("sp_data", spData);
        }
        Long skuSum = query.getSkuSum();
        if (skuSum != null) {
            qw.eq("sku_sum", skuSum);
        }
        String bhZt = query.getBhZt();
        if (!StringUtils.isEmpty(bhZt)) {
            qw.eq("bh_zt", bhZt);
        }
        Long deptGysId = query.getDeptGysId();
        if (deptGysId != null) {
            qw.eq("dept_gys_id", deptGysId);
        }
        LocalDateTime bhTime = query.getBhTime();
        if (bhTime != null) {
            qw.eq("bh_time", bhTime);
        }
        LocalDateTime qsTime = query.getQsTime();
        if (qsTime != null) {
            qw.eq("qs_time", qsTime);
        }
        Long memberId = query.getMemberId();
        if (memberId != null) {
            qw.eq("member_id", memberId);
        }
        Long lockId = query.getLockId();
        if (lockId != null) {
            qw.eq("lock_id", lockId);
        }
 
        String groupType = query.getGroupType();
        if(groupType!=null) {
        	if("1".equals(groupType)) {
        		qw.groupBy("dept_zbj_id,start_time,end_time,product_id,product_name,sku_id,sp_data");
        		qw.select("sum(sku_sum) as skuSum, sum(sale_price) as salePrice, dept_zbj_id as deptZbjId,start_time as startTime,end_time as endTime,product_id as productId,product_name as productName,sku_id as skuId , sp_data as spData");
        	}
        }
        return orderItemPrepareMapper.selectList(qw);
    }

    /**
     * 新增备货管理
     *
     * @param orderItemPrepare 备货管理
     * @return 结果
     */
    public int insert(OrderItemPrepare orderItemPrepare) {
        orderItemPrepare.setDelFlag(0);
        orderItemPrepare.setCreateTime(LocalDateTime.now());
        return orderItemPrepareMapper.insert(orderItemPrepare);
    }

    /**
     * 修改备货管理
     *
     * @param orderItemPrepare 备货管理
     * @return 结果
     */
    public int update(OrderItemPrepare orderItemPrepare) {
        return orderItemPrepareMapper.updateById(orderItemPrepare);
    }
    
    
    /**
     * 修改备货管理状态
     *
     * @param orderItemPrepare 备货管理
     * @return 结果
     */
    public int updateBhZt(OrderItemPrepareFrom orderItemPrepare) {
    	OrderItemPrepare dbOrderItemPrepare = orderItemPrepareMapper.selectById(orderItemPrepare.getId());
    	dbOrderItemPrepare.setBhZt(orderItemPrepare.getBhZt());
   
    	
    	OrderGysVo orderGysVo = new OrderGysVo();
    	orderGysVo.setUpdateTime(orderItemPrepare.getUpdateTime());
    	orderGysVo.setPrepareId(orderItemPrepare.getId());
    	orderGysVo.setSendGysStatus(Integer.parseInt(orderItemPrepare.getBhZt()));
    	if(orderGysVo.getSendGysStatus().intValue() == 3) {
    		orderGysVo.setDeliveryCompany(orderItemPrepare.getExpressmc());
    		orderGysVo.setDeliverySn(orderItemPrepare.getExpressbh());
        	dbOrderItemPrepare.setExpressmc(orderItemPrepare.getExpressmc());
        	dbOrderItemPrepare.setExpressbh(orderItemPrepare.getExpressbh());
    	}
    	if(orderGysVo.getSendGysStatus().intValue() == 4) {
        	dbOrderItemPrepare.setQsTime(orderItemPrepare.getQsTime());
        	dbOrderItemPrepare.setMemberId(orderItemPrepare.getMemberId());
    	}
    	orderMapper.orderGysStatus(orderGysVo);
        return orderItemPrepareMapper.updateById(dbOrderItemPrepare);
    }
    

    
    
    /**
     * 批量删除备货管理
     *
     * @param ids 需要删除的备货管理主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return orderItemPrepareMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除备货管理信息
     *
     * @param id 备货管理主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return orderItemPrepareMapper.updateDelFlagByIds(ids);
    }
}
